<template>
  <view class="container">
    <view class="center">
      <image class="w-180 h-180" src="@/static/logo.png"></image>
    </view>

    <view class="login-btn">
      <button v-if="agreement" class="lh-104 br-10 fs-32" style="height: 100%; background-color: #c5261d; color: #fff"
        @click="onLogin">
        <text>授权登录</text>
      </button>
      <view v-else @click="$u.toast('请同意用户协议与隐私政策')" class="lh-104"
        style="height: 100%; background-color: #ccc; border-radius: 10rpx">
        <text class="fs-32">授权登录</text>
      </view>
    </view>

    <view class="agreement">
      <image v-if="!agreement" @click="agreement = !agreement" src="@/static/select.png"></image>
      <image v-else @click="agreement = !agreement" src="@/static/is-select.png"></image>
      <text>阅读并同意<text @click="onGetXieyi(3)" style="color: #ae2c2c">《用户协议》</text>和<text @click="onGetXieyi(4)"
          style="color: #ae2c2c">《隐私政策》</text></text>
    </view>
    <setUserInfo ref="setUserInfoRef"></setUserInfo>
  </view>
</template>

<script>
import { authLoginApi } from "@/services/my";
import setUserInfo from "@/components/setUserInfo.vue";
export default {
  components: {
    setUserInfo,
  },
  data() {
    return {
      agreement: false,
    };
  },
  methods: {
    onLogin() {
      uni.login({
        provider: "weixin",
        success: (loginRes) => {
          const code = loginRes.code;
          authLoginApi({ code }).then((res) => {
            if (res.code == 1) {
              const { jump_type } = res.data;
              if (jump_type == 1) {
                //需要注册
                const { openid, session_key } = res.data?.wx_data;
                this.$refs.setUserInfoRef.showPopup({ openid, session_key });
              } else {
                uni.setStorageSync("token", res.data.userinfo.token);
				uni.setStorageSync('userId',res.data.userinfo.id )
                this.$store.dispatch("user/getUserInfo");
                this.$u.toast("登录成功");
                setTimeout(() => {
                  uni.switchTab({
                    url: "/pages/home/home",
                  });
                }, 1000);
              }
            }
          });
        },
      });
    },
    onGetXieyi(id) {
      this.$utils.to(`/pages/xieyi?id=${id}`);
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  padding: 100px 20rpx 0;
}

.login-btn {
  width: 686rpx;
  height: 104rpx;
  text-align: center;
  line-height: 104rpx;
  color: #fff;
  position: absolute;
  bottom: 700rpx;
  left: 50%;
  transform: translate(-50%);
}

.agreement {
  width: 100%;
  display: flex;
  align-items: center;
  font-size: 24rpx;
  position: fixed;
  left: 0;
  bottom: 100rpx;
  justify-content: center;
}

.agreement>image {
  width: 30rpx;
  height: 30rpx;
  margin-right: 20rpx;
}
</style>
